package stephen.jan21;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Login() {
        // TODO Auto-generated constructor stub
    	
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	
	
		
		String name =request.getParameter("username");
		String pwd=request.getParameter("pwd");
		System.out.println("name="+name+"  pwd="+pwd);
		UserService us=new UserService();
		boolean result=false;
		try {
			result=us.login(name,pwd);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
				if(result){
					response.sendRedirect("stephen.jan21/stephen.loginSuccess.html");
					}
				else{
					response.sendRedirect("aaLoginFail.html");
					}
			}
		
		
		//重定向response.sendRedirect("页面的地址")
		//response.sendRedirect("stephen.loginSucess.html");
		//response.sendRedirect("stephenlogfail.html");	
		//请求转发,地址不会变化
		//request.getRequestDispatcher("stephen.loginSucess.html").forward(request,response);
		//response.getWriter().append("Served at: ").append(request.getContextPath());


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
